#include "gamecontroller.h"
#include "constants.h"
#include "mainwindow.h"

#include <QKeyEvent>
#include <QDebug>
#include <QMessageBox>

#include "gamestatuswidget.h"
#include "speedstatuswidget.h"
#include "newhelpwidget.h"


const int DEF_CIRCAL = 30;
const int DENOMINATOR = 1000;

GameController::GameController(QGraphicsScene *scene, QObject *parent) :
    QObject(parent),
    m_scene(scene),
    m_snake(new Snake(*this)),
    m_circal(DEF_CIRCAL)
{
    m_timer.start(DENOMINATOR / m_circal);//游戏运行

    Food* food = new Food(0, -60);
    scene->addItem(food);
    scene->addItem(m_snake);

    scene->installEventFilter(this);

    connect(&m_timer, SIGNAL(timeout()), scene, SLOT(advance()));
    ///前进，刷新scene 内的所有item
    ////通过为场景中的所有项目调用QGraphicsItem :: advance（），通知所有item 即将发生的变化

    resume();
}

GameController::~GameController()
{
}

void GameController::resume()
{
    m_timer.start(DENOMINATOR / m_circal);
}

void GameController::pause()
{
    m_timer.stop();
}

bool GameController::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type() == QEvent::KeyPress) {
        handleKeyPressed((QKeyEvent*)event);
        return true;
    }else {
        return QObject::eventFilter(watched, event);
    }
}

void GameController::handleKeyPressed(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Left:
        m_snake->setMoveDirection(Snake::MoveLeft);
        break;
    case Qt::Key_Right:
        m_snake->setMoveDirection(Snake::MoveRight);
        break;
    case Qt::Key_Up:
        m_snake->setMoveDirection(Snake::MoveUp);
        break;
    case Qt::Key_Down:
        m_snake->setMoveDirection(Snake::MoveDown);
        break;

    case Qt::Key_Space://暂停
    {
        pause();

        GameStatusWidget widget;//这个不是策略模式，因为它没有遵守开闭原则
        AbstractMaskWidget * pWidget = &widget;
        pWidget->updateUI();
        pWidget->exec();

        resume();
    }

        break;
    case Qt::Key_Escape://退出
        exit(1);
        break;
    case Qt::Key_Plus://+
    case Qt::Key_Minus://-
    {
        chageSpeed(event->key());

        GameInfo info;
        info.GameSpeed = m_circal;
        SpeedStatusWidget hDialog(info);
        hDialog.resize(200, 200);
        hDialog.updateUI();

        QTimer::singleShot(1500, &hDialog, SLOT(accept()));
        hDialog.exec();
    }
        break;
    case Qt::Key_F1:
    {
        m_snake->setMoveDirection(Snake::NoMove);

        HelpWidget hDialog;
        AbstractMaskWidget * pWidget = &hDialog;
        pWidget->updateUI();
        pWidget->exec();
    }
        break;
    default:
        return;
    }
}

void GameController::snakeAteFood(Food *food)
{
    m_scene->removeItem(food);
    delete food;

    addNewFood();
}

void GameController::addNewFood()
{
    int x, y;
    do {
        x = (int) (qrand() % 100 ) / TILE_SIZE; //产生以内的随机数(0-100)
        y = (int) (qrand() % 100) / TILE_SIZE;

        x *= TILE_SIZE;
        y *= TILE_SIZE;

        //得到一个整数
    }while( m_snake->shape().contains(m_snake->mapFromScene(QPointF(x, y ))));//出现的食物不能在蛇的身体中

    Food * food = new Food(x, y);
    m_scene->addItem(food);
}

void GameController::snakeAteItSelf()
{
    QTimer::singleShot(0, this, SLOT(gameOver()));
}

void GameController::gameOver()
{
    pause();//暂停游戏
    int ret = QMessageBox::information(nullptr, "info", "Game Over, Whether to continue", QMessageBox::Ok | QMessageBox::No);
    m_scene->clear();
    if(ret == QMessageBox::No) {
        exit(1);
    }


    m_snake = new Snake(*this);
    m_scene->addItem(m_snake);

    addNewFood();
    resume();//恢复游戏
}


void GameController::chageSpeed( int type)
{
    if (type == Qt::Key_Plus) {
        m_circal +=5;
    }else {
        if (m_circal > DEF_CIRCAL) {
            m_circal -=5;
        }
    }

    m_timer.start(DENOMINATOR / m_circal);
}

bool GameController::getGameStatus()
{
    return m_timer.isActive();
}

int GameController::getGameSpeed()
{
    return m_circal;
}
